import config from '@/config/config.js';
import http from '@/api/http.js'

let bomRequest={
    loadBOMs(materialId,page=1,limit=10,callBack){
        // 获取token
        let token = localStorage.getItem("token");
        if(token){
            let url = config.mdBaseUrl + "/material/listBOM?materialId="+ materialId + "&page=" +page + "&limit=" + limit;
            http.get(url,callBack);
        }else{
            callBack(undefined);
        }
    }, save(materialOfId,materials,callBack){
        // 获取token
        let token = localStorage.getItem("token");
        if(token){
            let url = config.mdBaseUrl + "/material/saveBOM";
            http.post(url,{materialOfId:materialOfId,materialIds:materials},callBack);
        }else{
            callBack(undefined);
        }
    }, updateBomById(bom,callBack) {
        let token = localStorage.getItem("token");
        if (token) {
            let url = config.mdBaseUrl + "/material/updateBOM";
            http.put(url,bom,callBack);
        } else {
            callBack(undefined);
        }
    }, selectBomById(bomId,callBack) {
        let token = localStorage.getItem("token");
        if (token) {
            let url = config.mdBaseUrl + "/material/selectBOMById?bomId=" + bomId;
            http.get(url,callBack);
        } else {
            callBack(undefined);
        }
    }, deleteBomById(bomId,callBack){
        let token = localStorage.getItem("token");
        if (token) {
            let url = config.mdBaseUrl + "/material/deleteBOM?bomId=" + bomId;
            http.delete(url,callBack);
        } else {
            callBack(undefined);
        }
    }, deleteBoms(bomIds,callBack){
        let token = localStorage.getItem("token");
        if (token) {
            let url = config.mdBaseUrl + "/material/deleteBOMs?a=1";
            if(bomIds){
                bomIds.forEach(id=> url += '&bomId='+id);
            }
            http.delete(url,callBack);
        } else {
            callBack(undefined);
        }
    }
}

export default bomRequest;